(function(cuiPackage){	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"html",{
		title: "源代码",
		toggle: true,
		init: function(){
			
		},
		toggleF: function(toolitem){
			this.syncCode();
			$(toolitem).addClass("active");
			this.frame.hide();
			$(this.target).show();
		},
		toggleS: function(toolitem){
			$(toolitem).removeClass("active");
			$(this.target).hide();
			this.frame.show();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"bold",{
		title: "粗体",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('bold',false, "bold");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"italic",{
		title: "斜体",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('italic',false, "italic");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"strike",{
		title: "删除线",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('strikethrough',false, "strikethrough");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"alignleft",{
		title: "居左",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('JustifyLeft',false, "JustifyLeft");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"aligncenter",{
		title: "居中",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('JustifyCenter',false, "JustifyCenter");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"alignright",{
		title: "居右",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('JustifyRight',false, "JustifyRight");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"justify",{
		title: "对齐",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('JustifyFull',false, "JustifyFull");
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"indent",{
		title: "缩进",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('Indent');
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"outdent",{
		title: "较少缩进",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('Outdent');
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"list",{
		title: "序列列表",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('InsertOrderedList');
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"unolist",{
		title: "无序列表",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('insertUnorderedList');
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"fontname",{
		title: "字体",
		init: function(){
			var self = this;
			var wrapper = $("<div>").addClass("ceditor-dropdown");
			this.toolbar.append(wrapper);
			var offsettop = $("a.cui-editor-tbitem-fontname",this.toolbar).parent()[0].offsetTop;
			var offsetleft = $("a.cui-editor-tbitem-fontname",this.toolbar).parent()[0].offsetLeft;
			var h = $("a.cui-editor-tbitem-fontname",this.toolbar).outerHeight(true);
			wrapper.css("left",offsetleft).css("top",offsettop+h);
			var fontnames = ["宋体","微软雅黑","楷体","黑体","隶书","Arial","Arial Black","Tahoma"];
			var dl = $("<dl>");
			for(var i in fontnames){
				var dt = $("<a href='javascript:void(null);'>"+fontnames[i]+"</a>");
				dt.css("font-family",fontnames[i]);
				dt.click(function(){
					cuiPackage.CPlugin.call(self,"fontname","setFont",[$(this).html()]);
				}).hover(function(){
					$(this).addClass("hover");
				},function(){
					$(this).removeClass("hover");
				});
				dl.append(dt);
			}
			wrapper.append(dl);
			$("a.cui-editor-tbitem-fontname",this.toolbar).data("dropdown",wrapper);
		},
		
		handler: function(toolitem){
			this.focus();
			var dropdown = $(toolitem).data("dropdown");
			if(dropdown){
				dropdown.show();
			}
		},
		
		setFont: function(fontname){
			if (fontname && fontname != "") {
				this.focus();
				this.doc.execCommand('fontName', false, fontname);
				this.syncCode();
			}
		}
	});
	
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"fontcolor",{
		title: "字体颜色",
		init: function(){
			var self = this;
			var wrapper = $("<div>").addClass("ceditor-dropdown ceditor-dropdown-fontcolor");
			this.toolbar.append(wrapper);
			var offsettop = $("a.cui-editor-tbitem-fontcolor",this.toolbar).parent()[0].offsetTop;
			var offsetleft = $("a.cui-editor-tbitem-fontcolor",this.toolbar).parent()[0].offsetLeft;
			var h = $("a.cui-editor-tbitem-fontcolor",this.toolbar).outerHeight(true);
			wrapper.css("left",offsetleft).css("top",offsettop+h);
			var fontcolors = ['#ffffff', '#000000', '#eeece1', '#1f497d', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', '#ffff00',
								'#f2f2f2', '#7f7f7f', '#ddd9c3', '#c6d9f0', '#dbe5f1', '#f2dcdb', '#ebf1dd', '#e5e0ec', '#dbeef3', '#fdeada', '#fff2ca',
								'#d8d8d8', '#595959', '#c4bd97', '#8db3e2', '#b8cce4', '#e5b9b7', '#d7e3bc', '#ccc1d9', '#b7dde8', '#fbd5b5', '#ffe694',
								'#bfbfbf', '#3f3f3f', '#938953', '#548dd4', '#95b3d7', '#d99694', '#c3d69b', '#b2a2c7', '#b7dde8', '#fac08f', '#f2c314',
								'#a5a5a5', '#262626', '#494429', '#17365d', '#366092', '#953734', '#76923c', '#5f497a', '#92cddc', '#e36c09', '#c09100',
								'#7f7f7f', '#0c0c0c', '#1d1b10', '#0f243e', '#244061', '#632423', '#4f6128', '#3f3151', '#31859b', '#974806', '#7f6000'];
			for(var i in fontcolors){
				var colorspan = $("<a href='javascript:void(0)'></a>").addClass("fontcolor-item").data("color",fontcolors[i]);
				colorspan.css("background-color",fontcolors[i]);
				wrapper.append(colorspan);
				colorspan.click(function(){
					var fontcolor = $(this).data("color");
					if (fontcolor && fontcolor != "") {
						self.focus();
						self.doc.execCommand('forecolor', false, fontcolor);
						self.syncCode();
					}
				});
			}
			$("a.cui-editor-tbitem-fontcolor",this.toolbar).data("dropdown",wrapper);
		},
		
		handler: function(toolitem){
			this.focus();
			$(toolitem).addClass("active");
			var dropdown = $(toolitem).data("dropdown");
			if(dropdown){
				dropdown.show();
			}
		}
	});
	
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"fontsize",{
		title: "字体大小",
		init: function(){
			var self = this;
			var wrapper = $("<div>").addClass("ceditor-dropdown ceditor-dropdown-fontsize");
			this.toolbar.append(wrapper);
			var offsettop = $("a.cui-editor-tbitem-fontsize",this.toolbar).parent()[0].offsetTop;
			var offsetleft = $("a.cui-editor-tbitem-fontsize",this.toolbar).parent()[0].offsetLeft;
			var h = $("a.cui-editor-tbitem-fontsize",this.toolbar).outerHeight(true);
			wrapper.css("left",offsetleft).css("top",offsettop+h);
			//var fontsizes = ["10px","11px","12px","14px","16px","18px","20px","24px","36px"];
			var fontsizes = ["9px","10px","13px","16px","18px","24px","32px"];
			var dl = $("<dl>");
			for(var i in fontsizes){
				var dt = $("<a href='javascript:void(0)'></a>").addClass("fontsize-item").html(fontsizes[i]).attr("size",(parseInt(i)+1));
				dt.css({"font-size":fontsizes[i],"line-height":fontsizes[i]});
				dt.click(function(){
					cuiPackage.CPlugin.call(self,"fontsize","setFontSize",[$(this).attr("size")]);
				}).hover(function(){
					$(this).addClass("hover");
				},function(){
					$(this).removeClass("hover");
				});
				dl.append(dt);
			}
			wrapper.append(dl);
			$("a.cui-editor-tbitem-fontsize",this.toolbar).data("dropdown",wrapper);
		},
		
		handler: function(toolitem){
			this.focus();
			$(toolitem).addClass("active");
			var dropdown = $(toolitem).data("dropdown");
			if(dropdown){
				dropdown.show();
			}
		},
		
		setFontSize: function(fontsize){
			if (fontsize && fontsize != "") {
				/*if (window.getSelection) {
					var range = this.getSelection();
					var n = document.createElement("span");
					n.style.fontSize = fontsize;
					this.getSelection().getRangeAt(0).surroundContents(n);
					range.collapse(true);
				}else if (document.selection){
					var range = this.getSelection();
					if (range.text) {
						var span = document.createElement("span");
						span.style.fontSize = fontsize;
						span.innerHTML = range.text;
						this.getSelection().pasteHTML(span.outerHTML);
						range.collapse(true);
					}
				}*/
				this.focus();
				this.doc.execCommand('fontSize', false, fontsize);
				this.syncCode();
			}
		}
	});
	
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"horizontal",{
		title: "插入横线",
		init: function(){
			
		},
		handler: function(toolitem){
			this.focus();
			this.doc.execCommand('insertHorizontalRule');
			this.syncCode();
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"undo",{
		title: "撤销",
		init: function(){
			
		},
		handler: function(toolitem){
			this.focus();
			this.doc.execCommand('Undo');
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"redo",{
		title: "重做",
		init: function(){
			
		},
		handler: function(toolitem){
			this.doc.execCommand('Redo');
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"link",{
		title: "超链接",
		init: function(){
			cuiPackage.CPlugin.call(this,"link","create",[]);
			var toolitem = $("a.cui-editor-tbitem-link", this.toolbar);
			cuiPackage.CPlugin.call(this,"link","listen",toolitem);
		},
		create: function(){
			var popup = $("a.cui-editor-tbitem-link", this.toolbar).data("popup");
			if (!popup) {
				var popup = $("<div>").addClass("ceditor-popup");
				$("body").append(popup);
				$("a.cui-editor-tbitem-link", this.toolbar).data("popup",popup);
			}
			
			var title = $("<div>").addClass("popup-title").html("超级链接");
			var close = $("<div>").addClass("popup-title-close");
			var cont = $("<div>").addClass("popup-cont");
			title.append(close);
			
			popup.append(title);
			popup.append(cont);
			
			var self = this;
			close.click(function(){
				popup.fadeOut(500);
				$("a.cui-editor-tbitem-link", self.toolbar).removeClass("active");
			});
			
			cont.append('<div class="from-field"><label>链接地址：</label><input class="textinput" id="link_url"></div>'+
			'<div class="from-field"><label>链接标题：</label><input class="textinput" id="link_title"></div>'+
			'<div class="from-field"><label>是否弹出新窗口：</label><input type="checkbox" id="link_target"></div>'+
			'<div class="from-tool"><a href="javascript:void(0);" class="add">添加</a><a class="cancle" href="javascript:void(0);">取消</a></div>'
			);
		},
		
		listen: function(toolitem){
			var popup = toolitem.data("popup");
			$(".add",popup).click($.proxy(function(e) {
				 var url = $("#link_url",popup).val();
				 var title = $("#link_title",popup).val();
				 var checked = $("#link_target",popup).is(":checked");

				 var link = '';
				 if(checked){
				 	link = '<a href="'+url+'" title="'+title+'" target="_blank">'+title+'</a>';
				 }else{
				 	link = '<a href="'+url+'" title="'+title+'">'+title+'</a>';
				 }
				 if ($.browser.msie) {
				 	this.focus();
					this.getSelection().pasteHTML(link);
				 }
				 else {
				 	this.focus();
				 	this.doc.execCommand('inserthtml', false, link);
				 }
				 popup.fadeOut(500);
				 toolitem.removeClass("active");
			}, this));
			
			$(".cancle",popup).click($.proxy(function(e) {
				popup.fadeOut(500);
				toolitem.removeClass("active");
			},this));
		},
		
		handler: function(toolitem){
			this.focus();
			$(toolitem).addClass("active");
			var popup = $(toolitem).data("popup");
			if(popup){
				popup.show();
				var h = popup.outerHeight(true);
				var w = popup.outerWidth(true);
				popup.css({"margin-top": -h/2,"margin-left": -w/2});
			}
		}
	});
	
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"unlink",{
		title: "取消超链接",
		init: function(){
			
		},
		
		handler: function(toolitem){
			this.focus();
			this.doc.execCommand('Unlink');
		}
	});
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"insertimg",{
		title: "插入图片",
		init: function(){
			cuiPackage.CPlugin.call(this,"insertimg","create",[]);
			var toolitem = $("a.cui-editor-tbitem-insertimg", this.toolbar);
			cuiPackage.CPlugin.call(this,"insertimg","listen",toolitem);
		},
		
		create: function(){
			var popup = $("a.cui-editor-tbitem-insertimg", this.toolbar).data("popup");
			if (!popup) {
				var popup = $("<div>").addClass("ceditor-popup");
				$("body").append(popup);
				$("a.cui-editor-tbitem-insertimg", this.toolbar).data("popup",popup);
			}
			
			var title = $("<div>").addClass("popup-title").html("插入图片");
			var close = $("<div>").addClass("popup-title-close");
			var cont = $("<div>").addClass("popup-cont");
			title.append(close);
			
			popup.append(title);
			popup.append(cont);
			
			var self = this;
			close.click(function(){
				popup.fadeOut(500);
				$("a.cui-editor-tbitem-insertimg", self.toolbar).removeClass("active");
			});
			
			cont.append('<div class="from-field"><label>图片地址：</label><input class="textinput" id="image_url"></div>'+
			'<div class="from-field"><label>图片标题：</label><input class="textinput" id="image_title"></div>'+
			'<div class="from-tool"><a href="javascript:void(0);" class="add">插入</a><a class="cancle" href="javascript:void(0);">取消</a></div>'
			);
		},
		
		listen: function(toolitem){
			var popup = toolitem.data("popup");
			$(".add",popup).click($.proxy(function(e) {
				var imgurl = $("#image_url").val();
				var image_title = $("#image_title").val();
				
				var img = $("<img>").attr({
					src: imgurl,
					alt: image_title
				});
				
				if ($.browser.msie) {
				 	this.focus();
					this.getSelection().pasteHTML(img[0].outerHTML);
				 }
				 else {
				 	this.focus();
				 	this.doc.execCommand('inserthtml', false, img[0].outerHTML);
				 }
				 popup.fadeOut(500);
				 toolitem.removeClass("active");
				 this.observeImages();
			}, this));
			
			$(".cancle",popup).click($.proxy(function(e) {
				popup.fadeOut(500);
				toolitem.removeClass("active");
			},this));
		},
		
		handler: function(toolitem){
			this.focus();
			$(toolitem).addClass("active");
			var popup = $(toolitem).data("popup");
			if(popup){
				popup.show();
				var h = popup.outerHeight(true);
				var w = popup.outerWidth(true);
				popup.css({"margin-top": -h/2,"margin-left": -w/2});
			}
		}
	});
	
	
	cuiPackage.CPlugin.add(cuiPackage.CEditor,"print",{
		title: "打印",
		init: function(){
			
		},
		handler: function(toolitem){
			this.focus();
			this.doc.execCommand('print');
		}
	});
})(jQuery.fn);